package com.io;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * 读取Properties文件
 * @author zhuangqi
 * @date 2017/5/18.
 */
public class ReadProperties {

    public static void main(String[] args) {
        readFunction2();
    }

    public static void readFunction1(){
        Properties prop = new Properties();
        try {
            prop.load(new FileInputStream("src/main/java/com/io/test.properties"));
            System.out.println(prop.getProperty("database"));
            System.out.println(prop.getProperty("dbuser"));
            System.out.println(prop.getProperty("dbpassword"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void readFunction2(){ //将properties文件放在class同一目录下ClassLoader能够扫描的到
        Properties prop = new Properties();
        try {
            prop.load(ReadProperties.class.getClassLoader().getResourceAsStream("test.properties"));

            System.out.println(prop.getProperty("database"));
            System.out.println(prop.getProperty("dbuser"));
            System.out.println(prop.getProperty("dbpassword"));
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
}
